<template>
  <div class="wechat-bind">
    <h1>修改密码</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input type="password" v-model="ruleForm.oldPassword"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="password2">
        <el-input type="password" v-model="ruleForm.password2"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    name:'site-setting',
    data() {

      var validatePass2 = (rule, value, callback) => {
        if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        apiUrl:apiUrl,
        ruleForm: {
          oldPassword: '',
          password: '',
          password2: '',
        },
        rules: {
          oldPassword: [
            { required: true, message: '请输入旧密码', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
          ],
          password2: [
            { required: true, message: '请输入确认新密码', trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur' },
          ]
        }
      };
    },

    methods: {

      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var url = this.apiUrl+'User/resetPassword';
            this.$http.post(url, this.ruleForm).then(response => {
              console.log(response.data)
              if(response.data.status==1){
                this.$message.success(response.data.msg);
              }else{
                this.$message.error(response.data.msg);
              }
              this.$refs[formName].resetFields();
            }, response => {
              this.$message.error('保存失败');
              console.log(response);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
